package com.java.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.param.AddressListParam;
import com.java.pojo.Address;
import com.java.user.mapper.AddressMapper;
import com.java.user.service.AddressService;
import com.java.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;


    /**
     * 根据用户id进行地址查找
     * @param userId
     * @return 地址信息
     */
    @Override
    public R list(Integer userId) {
        //根据传入参数，查询数据库数据
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Address> addressList = addressMapper.selectList(queryWrapper);
        log.info("查询地址结果{}",addressList);
        return R.ok("查询成功",addressList);
    }

    /**
     * 保存地址信息
     * @param address 地址
     * @return 返回全部地址信息
     */
    @Override
    public R save(Address address) {
        //插入数据
        int rows = addressMapper.insert(address);
        if(rows==0){
            log.info("数据插入失败！");
            return R.fail("数据保存失败！");
        }

        //返回数据
        return list(address.getUserId());
    }

    /**
     * 删除地址数据信息
     * @param id 用户id
     * @return 返回001，004
     */
    @Override
    public R remove(Integer id) {
        int rows = addressMapper.deleteById(id);
        if(rows==0){
            log.info("地址信息删除失败！");
            return R.fail("地址信息删除失败！");
        }
        return R.ok("地址信息删除成功！");
    }

}
